<template>
  <div class="common-table">
    <!--表格区域-->
    <el-table
      v-loading="tableConfig.loading"
      ref="commonTable"
      :element-loading-text="tableConfig.loadingText"
      :stripe="tableConfig.stripe"
      :data="tableData"
      :header-row-style="headerRowStyle"
      :header-cell-style="headerCellStyle"
      :row-style="rowStyle"
      :cell-style="cellStyle"
      :row-class-name="rowClassName"
      :height="tableHeight"
      :max-height="tableMaxHeight"
      :span-method="arraySpanMethod"
      :default-sort="tableConfig.defaultSort"
      border
      @sort-change="changeSort"
      @row-click="rowClick"
      @row-contextmenu="rightClick"
      @row-dblclick="dblclick">
      <!--表格行选择框，可配置是否生成-->
      <el-table-column v-if="tableConfig.rowSelect" type="selection" width="45" />
      <!--表格序号列，可配置是否生成-->
      <el-table-column v-if="tableConfig.orderNum" label="序号" width="60" fixed>
        <template slot-scope="scope">
          <span>{{ (tableConfig.page - 1) * tableConfig.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <!--表格内容列-->
      <el-table-column
        v-for="item in tableLabel"
        :key="item.prop"
        :label="item.label"
        :property="item.prop"
        :sortable="item.sortable"
        :fixed="item.fixed"
        :min-width="item.width ? item.width : 125"
        :show-overflow-tooltip="tableConfig.showTooltip === false ? false : true">
        <template slot-scope="scope">
          <!--作用域插槽，用于需要自定义展示和绑定方法的列-->
          <slot v-if="item.slot" :name="item.prop" :row="scope.row" :index="scope.$index" />
          <span v-else>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>
    </el-table>
    <context-menu :data-m="tableConfig.rightMenuData" :show-v.sync="rightMenuShow" :target="rightMenuTarget" @rowClick="rightMenuClick" />
    <!-- 表格左下角操作类容插槽 -->
    <el-pagination v-if="tableConfig.rightSlot" class="legendsPagination" layout="slot">
      <slot name="rightSlot"/>
    </el-pagination>
    <!-- 表格左下角文字描述 -->
    <el-pagination v-if="tableConfig.description" style="margin-top: 4px" class="legendsPagination" layout="slot">
      <ul>
        <li v-for="item in tableConfig.description" :key="item.name">
          <div :style="`color:${item.symbolColor}`" class="describeDiv">{{ item.symbol }}</div>
          <div :style="`color:${item.nameColor}`" class="describeText">{{ item.name }}</div>
        </li>
      </ul>
    </el-pagination>
    <!--分页区域-->
    <!--左侧图例区域，可控制是否生成-->
    <el-pagination v-if="tableConfig.legends" class="legendsPagination" layout="slot">
      <ul>
        <li v-for="item in tableConfig.legends" :key="item.name">
          <div :style="`background-color:${item.color}`" class="legendsDiv" />
          <div class="legendsText">{{ item.name }}</div>
        </li>
      </ul>
    </el-pagination>
    <!--左侧字体区，可控制是否生成-->
    <el-pagination v-if="tableConfig.total" style="margin-top: 4px" class="legendsPagination" layout="slot">
      <ul>
        <li v-for="item in tableConfig.total" :key="item.name">
          <div class="legendsText">
            <span :style="`color:${item.namecolor}`">{{ item.name }}:</span>
            <span :style="`color:${item.numcolor}`">{{ item.num }}</span>
          </div>
        </li>
      </ul>
    </el-pagination>
    <!--分页跳转区域-->
    <el-pagination
      v-if="tableConfig.pagination"
      :current-page.sync="tableConfig.page"
      :page-size.sync="tableConfig.pageSize"
      :total="tableConfig.totalCount"
      class="pagePagination"
      layout="slot, prev, pager, next"
      @current-change="handleCurrentChange">
      <div class="pageStyle">
        <span v-if="tableConfig.tableRefresh" class="tableRefresh" @click="tableRefresh"><i class="el-icon-refresh" /></span>
        <!-- <span v-if="tableConfig.countShow === false ? false : true">
          当前显示
          <span class="numberText">{{ tableConfig.count }}</span>
          条，
        </span> -->
        <span v-if="tableConfig.totalShow === false ? false : true" style="margin-right: 15px;">
          共当前记录
          <span class="numberText">{{ tableConfig.totalCount }}</span>
          条
        </span>
        <!-- <span v-if="tableConfig.pageSelectShow === false ? false : true" class="selectText">
          <span>每页</span>
          <el-select v-model="pageSize" popper-class="pageSizeSelect" @change="handleSizeChange">
            <el-option v-for="item in tableConfig.pageSizeOptions" :key="item" :label="item" :value="item" />
          </el-select>
          <span>条</span>
        </span> -->
      </div>
    </el-pagination>
    <div style="clear: both;" />
  </div>
</template>

<script>
import contextMenu from '../context-menu/index'

export default {
  components: {
    contextMenu
  },

  props: {
    tableData: {
      type: Array,
      default: function () {
        return []
      }
    },
    tableLabel: {
      type: Array,
      default: function () {
        return []
      }
    },
    tableLabelOperation: {
      type: Array,
      default: function () {
        return []
      }
    },
    tableConfig: {
      type: Object,
      default: function () {
        return {}
      }
    },
    // 合并行列
    arraySpanMethod: {
      type: Function,
      default: null
    }
  },

  data () {
    return {
      rightMenuShow: false,
      rightMenuTarget: null,
      rightClickRow: {},
      callbackSpan: {},
      pageSize: this.tableConfig.pageSize,
      multipleSelection: [],
      freshChecked: false
    }
  },
  computed: {
    tableHeight () {
      return this.tableConfig.tableHeight
    },
    tableMaxHeight () {
      return this.tableConfig.tableMaxHeight
    }
  },
  watch: {
    tableHeight (oldValue, newVaue) {},
    tableMaxHeight (oldValue, newVaue) {}
  },

  methods: {
    // 表头行样式，用于控制表头高度，字体大小
    headerRowStyle () {
      return this.tableConfig.headerSize
    },
    // 表头单元格样式，用于控制表头背景颜色，字体颜色
    headerCellStyle () {
      return this.tableConfig.headerColor
    },
    // 表格行样式，用于控制表格高度，字体大小
    rowStyle ({ row, rowIndex }) {
      return Object.assign(this.tableConfig.rowSize, this.tableConfig.rowColor)
    },
    // 表格单元格样式，用于表格背景颜色，字体颜色
    cellStyle (val) {
      let cellColor = {}
      // 如果配置了给单元格单独设置颜色
      if (this.tableConfig.setCellColor) {
        this.$emit('set-cell-color', val, cellColorCallback => {
          // 如果回调函数返回了单独设置的颜色
          if (cellColorCallback) {
            cellColor = cellColorCallback
          }
        })
        return cellColor
      }
    },
    // 每页条数发生改变时触发
    changeSort ({ column, prop, order }) {
      this.$emit('handle-change-sort', { column, prop, order })
    },
    // 每页条数发生改变时触发
    handleSizeChange (val) {
      this.$emit('handle-size-change', val)
    },
    // 当前页发生改变时触发
    handleCurrentChange (val) {
      this.$emit('handle-current-change', val)
    },
    // // 行多选框发生改变时触发，将选中行数据返回给父组件处理
    // handleSelectionChange (row) {
    //   this.$emit('handle-selection-change', row)
    //   this.multipleSelection = []
    //   if (row.length > 0) {
    //     row.forEach((value, index) => {
    //       this.multipleSelection.push(value[this.tableConfig.uniqueId])
    //     })
    //   }
    // },
    rowClassName ({ row, rowIndex }) {
      let color = ''
      for (const item of this.multipleSelection.values()) {
        if (item === row[this.tableConfig.uniqueId]) {
          color = 'row-selected-color'
        }
      }
      return color
    },

    rowClick (row) {
      if (this.tableConfig.clickRow === true) {
        this.$refs.commonTable.clearSelection()
        this.$refs.commonTable.toggleRowSelection(row)
      }
    },

    rightClick (row, column, event) {
      if (this.tableConfig.rightMenu === true) {
        event.preventDefault()
        this.rightMenuShow = true
        this.rightMenuTarget = event
        this.rightClickRow = row
        this.$refs.commonTable.clearSelection()
        this.$refs.commonTable.toggleRowSelection(row)
      }
    },
    rightMenuClick (val) {
      this.rightMenuShow = false
      this.$emit('right-menu-click', this.rightClickRow, val)
    },
    tableRefresh () {
      this.$emit('table-refresh')
    },
    // 某行被双击
    dblclick (row, column, event) {
      this.$emit('dblclick', row)
    }
  }
}
</script>

<style lang="less">
.common-table {
  .operation {
    .verticalLine {
      color: #999999;
      float: left;
      line-height: 34px;
      margin: 0px 10px 0px 10px;
    }
    .el-button {
      padding: 8px;
      float: left;
      color: #666666;
      font-size: 14px;
      font-family: Source Han Sans CN;
      &:focus {
        border: 1px solid #999999 !important;
      }
    }
  }
  .el-table {
    background-color: #ffffff;
    .el-table__header tr,
    .el-table__header th {
      color: #333333;
      padding: 0px;
      text-align: center;
      font-family: Source Han Sans CN;
      font-weight: bold;
    }
    .el-table__body tr,
    .el-table__body td {
      text-align: center;
      padding: 0px;
      cursor: default;
    }
    .el-checkbox__input.is-checked .el-checkbox__inner,
    .el-checkbox__input.is-indeterminate .el-checkbox__inner {
      background-color: #019492;
      border-color: #019492;
    }
  }
  .el-table--striped .el-table__body tr.el-table__row--striped td {
    background-color: #ffffff;
  }
  .row-selected-color {
    td {
      background-color: #e0f2f2 !important;
    }
  }
  .legendsPagination {
    float: left;
    margin-top: 10px;
    padding-left: 0px;
    ul {
      margin-top: 5px;
      padding: 0px;
      li {
        list-style: none;
        float: left;
        .describeDiv {
          float: left;
          margin: 1px 4px 1px 0px;
        }
        .describeText{
          float: left;
          height:15px;
          font-family:SourceHanSansCN-Regular;
          color:#666666;
          line-height:20px;
          margin-right: 15px;
          font-size: 14px;
          font-weight: initial;
        }
        .legendsDiv {
          width: 14px;
          height: 14px;
          float: left;
          margin: 1px 4px 1px 0px;
        }
        .legendsText {
          float: left;
          margin-right: 15px;
          font-size: 14px;
          font-family: Source Han Sans CN;
          color: #999999;
          font-weight: bold;
        }
      }
    }
  }
  .pagePagination {
    float: right;
    display: flex;
    justify-content: flex-end;
    margin-top: 10px;
    .pageStyle {
      color: #666666;
      margin-right: 20px;
      span {
        min-width: 10px !important;
        font-size: 13px;
        font-family: Source Han Sans CN;
        font-weight: 400;
      }
      .numberText {
        color: #29817d;
        text-align: center;
        padding: 0px 4px 0px 4px;
      }
      .selectText {
        color: #666666;
        font-weight: 400;
        span {
          font-size: 14px;
        }
        .el-select {
          .el-input {
            font-size: 14px;
            width: 50px !important;
            .el-input__inner {
              padding: 10px;
              height: 24px;
              text-align: left;
              font-size: 14px;
              font-family: Source Han Sans CN;
              font-weight: 400;
              color: #333333;
            }
            .el-input__icon {
              line-height: 28px;
            }
          }
        }
      }
      .tableRefresh {
        margin-right: 20px;
        color: #0095ff;
        font-size: 16px !important;
        cursor: pointer;
      }
    }
    .el-pager {
      font-size: 16px;
      font-family: Source Han Sans CN;
      color: #999999;
      li {
        vertical-align: middle;
      }
    }
  }
}
.pageSizeSelect {
  min-width: 50px !important;
  z-index: 9999 !important;
  .el-select-dropdown__list .el-select-dropdown__item.hover,
  .el-select-dropdown__item.selected {
    min-width: 50px !important;
  }
  .el-select-dropdown__item {
    height: 28px;
    line-height: 28px;
    padding: 0px 18px 0px 15px;
    font-size: 14px;
    font-family: Source Han Sans CN;
    color: #333333;
  }
  .popper__arrow {
    left: 30px !important;
  }
}
.el-tooltip__popper {
  margin-bottom: 0px !important;
  background-color: #ffffff !important;
  color: #333333 !important;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  .popper__arrow {
    border: 0px !important;
    border-top-color: #ffffff !important;
  }
  .popper__arrow::after {
    border: 0px !important;
    border-top-color: #ffffff !important;
  }
}
</style>
